package com.prs.crm.action.hr;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.crm.CustomerLevel;
import com.prs.crm.domain.crm.CustomerType;
import com.prs.crm.domain.hr.RoleType;
import com.prs.crm.service.hr.RoleTypeService;
import com.prs.crm.service.sys.LogService;

public class RoleTypeAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private RoleType roleType;
	private RoleTypeService roleTypeService;
	private PaginationSupport pagination;
	private Integer[] roleTypeIds;
	private LogService logService;

	@Action("listRoleType")
	public String execute() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				RoleType.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getRoleTypeService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("selectRoleType")
	public String selectRoleType() {
		this.execute();
		return SUCCESS;
	}

	@Action("editRoleType")
	public String edit() {
		return SUCCESS;
	}

	@Action(value = "saveRoleType", results = {
			@Result(name = "success", type = "redirect", location = "listRoleType"),
			@Result(name = "error", type = "chain", location = "editRoleType") })
	public String save() {
		if (roleType.getName() == null || "".equals(roleType.getName().trim())) {
			addActionError("类型名称不能为空");
			return ERROR;
		}
		if (this.getLogService().getSearchName(RoleType.class,
				roleType.getName())) {
			this.addActionError("名称已经存在");
			return ERROR;
		}
		this.getRoleTypeService().save(this.roleType);
		return SUCCESS;
	}

	@Action(value = "removeRoleType", results = {
			@Result(name = "success", type = "redirect", location = "listRoleType"),
			@Result(name = "error", type = "chain", location = "listRoleType") })
	public String remove() {
		if (roleTypeIds == null) {
			this.addActionError("请选择要删除的职位类型");
			return ERROR;
		}
		for (Integer id : roleTypeIds) {
			RoleType type = this.getRoleTypeService().get(id);
			if (type.getRoles().size() != 0) {
				this.addActionError("已有客户引用，不能删除");
				return ERROR;
			}
		}
		this.getRoleTypeService().batchRemove(this.getRoleTypeIds());
		return SUCCESS;
	}

	public RoleType getRoleType() {
		return roleType;
	}

	public void setRoleType(RoleType roleType) {
		this.roleType = roleType;
	}

	public RoleTypeService getRoleTypeService() {
		return roleTypeService;
	}

	public void setRoleTypeService(RoleTypeService roleTypeService) {
		this.roleTypeService = roleTypeService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public void setRoleTypeIds(Integer[] roleTypeIds) {
		this.roleTypeIds = roleTypeIds;
	}

	public Integer[] getRoleTypeIds() {
		return roleTypeIds;
	}

	public LogService getLogService() {
		return logService;
	}

	public void setLogService(LogService logService) {
		this.logService = logService;
	}
}
